package com.bora_bandeco;

import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;

public class ContatosActivity extends SlidingFragmentActivity {

	public FragmentManager mFragmentManager;
	private ContatosListAdapter adapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.mFragmentManager = getSupportFragmentManager();

		ActionBar actionBar = this.getSupportActionBar();
		actionBar.setCustomView(R.layout.actionbar);
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setLogo(R.drawable.actionbar_logo);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setHomeButtonEnabled(true);

        SlidingMenu slidingMenu = getSlidingMenu();
        slidingMenu.setBehindWidthRes(R.dimen.sliding_menu_width);
        slidingMenu.setBehindScrollScale(0.0f);
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        setBehindContentView(R.layout.fragment_menu);

        //Esconde o ícone de notificação
        ImageView notification_icon = (ImageView)actionBar.getCustomView().findViewById(R.id.notification);
        notification_icon.setVisibility(View.GONE);
        		
        TextView titulo = (TextView)actionBar.getCustomView().findViewById(R.id.title);
        titulo.setText(R.string.selecionar_convidados);
        
        ImageView actionbar_icon = (ImageView)actionBar.getCustomView().findViewById(R.id.actionbar_icon);
        actionbar_icon.setImageDrawable(getResources().getDrawable(R.drawable.ic_left_quote));
        
        //Faz voltar a activity
        actionbar_icon.setOnClickListener(new OnClickListener() {
	        @Override
			public void onClick(View view) {
	        	ContatosActivity.this.finish();
	        }
	    });
        
        setContentView(R.layout.activity_contatos);
        getSupportFragmentManager().beginTransaction().replace(R.id.frag_menu, new MenuFragment()).commit();
        
        
        this.carregarContatos();
        
        Button confirmar = (Button) findViewById(R.id.confirmar);
        confirmar.setOnClickListener(new OnClickListener() {
	        @Override
			public void onClick(View view) {
	        	
	        	NovoConviteActivity.adapter.clear();
	        	
	        	for (int position = 0; position < adapter.getCount(); position++) {
	        		if (adapter.isChecked(position)) {
	        			Contato c = (Contato)adapter.getItem(position);
		        		NovoConviteActivity.adapter.addItem(c);
	        		}
	        	}
	        	
	        	ContatosActivity.this.finish();
	        }
	    });
	}
	
	private void carregarContatos() {
		String[] projection = { "_id", "nome", "foto" };
		
		TextView label = (TextView) findViewById(R.id.label);
        GridView grid = (GridView) findViewById(R.id.gridview);
        this.adapter = new ContatosListAdapter(this);
		
        //Busca todos os contatos
		Uri uri = BoraBandecoProvider.CONTENT_URI_CONTATOS;
		Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
		
		if (cursor != null && cursor.getCount() > 0) {
			cursor.moveToFirst();
			
			for (int i = 0; i < cursor.getCount(); i++) {
				int id = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
		        
				Contato contato = new Contato(id);
				
				this.adapter.addItem(contato);
		        
		        cursor.moveToNext();
			}
			
			cursor.close();
			
			label.setVisibility(View.GONE);
		} else {
			label.setText(R.string.nao_ha_contatos);
			label.setVisibility(View.VISIBLE);
		}
		

		grid.setAdapter(this.adapter);
	}

}
